package mybinarytree;

import com.sun.source.tree.Tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class MyBinaryTree {

    public static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = H;

        return A;
    }

    public void preOrder(TreeNode root) {
        if (root == null) {
            return ;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);//遍历左子树
        preOrder(root.right);//遍历右子树
    }

    public void inOrder(TreeNode root) {
        if (root == null) {
            return ;
        }
        inOrder(root.left);//遍历左子树
        System.out.print(root.val + " ");
        inOrder(root.right);//遍历右子树
    }

    public void postOrder(TreeNode root) {
        if (root == null) {
            return ;
        }
        postOrder(root.left);//遍历左子树
        postOrder(root.right);//遍历右子树
        System.out.print(root.val + " ");
    }

    public int treeSize(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return treeSize(root.left) + treeSize(root.right) + 1;
    }

    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    public int getLinearNodeCount (TreeNode root , int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getLinearNodeCount(root.left,k-1) +
                getLinearNodeCount(root.right , k-1);
    }

    public int getHigh (TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = getHigh(root.left);
        int right = getHigh(root.right);
        return Math.max(left,right) + 1;
    }

    public TreeNode find (TreeNode root , char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode ret =  find(root.left , val);
        if (ret != null) {
            return ret;
        }
        ret =  find(root.right , val);
        if (ret != null) {
            return ret;
        }
        return null;
    }

    public void linearOrder (TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return true;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur == null) {
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while (!queue.isEmpty()) {
            TreeNode node = queue.peek();
            if (node != null) {
                break;
            } else {
                queue.poll();
            }
        }
        return true;
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if (root == p || root == q) {
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left , p , q);
        TreeNode rightTree = lowestCommonAncestor(root.right , p , q);
        if (leftTree != null && rightTree != null) {
            return root;
        } else if (leftTree != null) {
            return leftTree;
        } else {
            return rightTree;
        }
    }

    public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return root;
        }
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root, p, stackP);
        getPath(root, q, stackQ);
        int count = Math.abs(stackP.size() - stackQ.size());
        if (stackP.size() > stackQ.size()) {
            for (int i = 0; i < count; i++) {
                stackP.pop();
            }
        } else if (stackP.size() < stackQ.size()){
            for (int i = 0; i < count; i++) {
                stackQ.pop();
            }
        }
        while (!stackP.isEmpty()) {
            if (stackP.peek() == stackQ.peek()) {
                return stackP.peek();
            }
            stackP.pop();
            stackQ.pop();
        }
        return null;
    }
    public boolean getPath (TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if (root == null) {
            return false;
        }
        stack.push(root);
        if (root == node) {
            return true;
        }
        boolean flg= getPath(root.left,node,stack);
        if (flg) {
            return true;
        }
        boolean flg1 = getPath(root.right, node, stack);
        if (flg1) {
            return true;
        }
        stack.pop();
        return false;
    }

    public void preOrderNot(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    public void inOrderNot(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.println(top.val + " ");
            cur = top.right;
        }
    }

    public void postOrderNot(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if (top.right == null || top.right == prev) {
                stack.pop();
                System.out.print(top.val + " ");
                prev = top;
            } else {
                cur = top.right;
            }
        }
    }
}



















